<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>剖析Vue的响应式 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Vue的响应式 Vue的响应式是Vue的核心原理，就当数据改变了页面会重新渲染。">
<meta property="og:type" content="article">
<meta property="og:title" content="剖析Vue的响应式">
<meta property="og:url" content="http://yoursite.com/2020/06/15/%E5%89%96%E6%9E%90Vue%E7%9A%84%E5%93%8D%E5%BA%94%E5%BC%8F/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="Vue的响应式 Vue的响应式是Vue的核心原理，就当数据改变了页面会重新渲染。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-06-15T10:32:18.000Z">
<meta property="article:modified_time" content="2020-07-30T13:04:01.653Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-剖析Vue的响应式" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/06/15/%E5%89%96%E6%9E%90Vue%E7%9A%84%E5%93%8D%E5%BA%94%E5%BC%8F/" class="article-date">
  <time datetime="2020-06-15T10:32:18.000Z" itemprop="datePublished">2020-06-15</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      剖析Vue的响应式
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="Vue的响应式"><a href="#Vue的响应式" class="headerlink" title="Vue的响应式"></a>Vue的响应式</h2><ul>
<li><p>Vue的响应式是Vue的核心原理，就当数据改变了页面会重新渲染。</p>
<a id="more"></a>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span>&#123;&#123; MrLiu.name &#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 页面显示：小刘 --&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> vm = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    el:<span class="string">&#x27;#app&#x27;</span>,</span><br><span class="line">    data :&#123;</span><br><span class="line">        msg:<span class="string">&#x27;一心一意&#x27;</span></span><br><span class="line">        MrLiu:&#123;</span><br><span class="line">            name:<span class="string">&#x27;小刘&#x27;</span>,</span><br><span class="line">            age:<span class="number">20</span>,</span><br><span class="line">            height:<span class="string">&#x27;170cm&#x27;</span></span><br><span class="line">        &#125; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//更改Vue的数据</span></span><br><span class="line">vm.MrLiu.name = <span class="string">&#x27;小王&#x27;</span> <span class="comment">//此时页面显示小王</span></span><br></pre></td></tr></table></figure>
<p>当我们对MrLiu的name值修改时会页面会重新渲染，这就是Vue的响应式。</p>
</li>
<li><p>那么问题来了，Vue是如何知道数据变了,监控数据的变化,从而去渲染页面</p>
<blockquote>
<p>在Vue2.0利用Object.defineProperty对数据进行劫持</p>
</blockquote>
</li>
</ul>
<h5 id="对与某些的数据更改不能够渲染页面"><a href="#对与某些的数据更改不能够渲染页面" class="headerlink" title="对与某些的数据更改不能够渲染页面"></a>对与某些的数据更改不能够渲染页面</h5><ul>
<li>1.未经声明的<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span>&#123;&#123; MrLiu.weight &#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 页面显示：小刘 --&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//更改未经声明的</span></span><br><span class="line">vm.MrLiu.weight = <span class="string">&#x27;150kg&#x27;</span> <span class="comment">//页面也不会重新渲染</span></span><br></pre></td></tr></table></figure></li>
</ul>
<p>  2.未经使用的(没有在Dom区域使用的数据)<br> <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//.未经使用的</span></span><br><span class="line">vm.MrLiu.age= <span class="string">&#x27;21&#x27;</span> <span class="comment">//页面也不会重新渲染,如果渲染页面会出现 150kg</span></span><br></pre></td></tr></table></figure><br>因为操作Dom是非常消耗性能的,Vue实现了尽可能的少操作Dom</p>
<ol start="3">
<li>数组<blockquote>
<p>通过索引更改数组<br>更改长度</p>
</blockquote>
</li>
<li>对象<blockquote>
<p>增删 对象</p>
</blockquote>
<ul>
<li>数组:</li>
</ul>
<ol>
<li>变异方法更改数据: push、pop、shift、unshift、splice、sort、reverse</li>
<li>vm.$set(要改谁,改什么,改成啥) Vue.set() 一样</li>
<li>vm.$delete(‘要删除谁的值’,’删哪个’) 删除数组的某一项</li>
</ol>
<ul>
<li>对象</li>
</ul>
<ol>
<li>vm.$set(要改谁,改什么,改成啥) Vue.set() 一样</li>
<li>vm.$delete(‘要删除谁的值’,’删哪个’) 删除数组的某一项<h5 id="更改数据后-页面立刻会重新渲染页面吗"><a href="#更改数据后-页面立刻会重新渲染页面吗" class="headerlink" title="更改数据后,页面立刻会重新渲染页面吗?"></a>更改数据后,页面立刻会重新渲染页面吗?</h5>不会的。<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">  <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span> ;i&lt;<span class="number">10000</span>;i++)&#123;</span><br><span class="line">      vm.age = i; <span class="comment">// 输出 9999 页面渲染一次</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//如果按照正常的逻辑 vm.age会被渲染10k次,这样的话我们的页面可能要卡得要死!!!</span></span><br><span class="line"><span class="string">``</span><span class="string">` </span></span><br><span class="line"><span class="string"> 因为页面渲染是一个*异步*的任务,是一个*微任务*,它会等待循环10k次,vm.age也会被赋值10k次, 然后去渲染页面(执行微任务)</span></span><br><span class="line"><span class="string"> ##### 同步执行栈 异步队列</span></span><br><span class="line"><span class="string"> &gt; 浏览器首先执行 同步执行栈,执行完后会去执行异步队列中的微任务,宏任务</span></span><br><span class="line"><span class="string"> `</span><span class="string">``</span>js</span><br><span class="line">   vm.msg = <span class="string">&#x27;两全其美&#x27;</span>;</span><br><span class="line">   <span class="built_in">console</span>.log(vm.msg,vm.$el.innerText) <span class="comment">// 输出 两全其美  一心一意</span></span><br><span class="line">   vm.msg = <span class="string">&#x27;五光十色&#x27;</span>;</span><br><span class="line">   <span class="built_in">console</span>.log(vm.msg,vm.$el.innerText) <span class="comment">// 输出 五光十色  一心一意</span></span><br><span class="line">   vm.msg = <span class="string">&#x27;七上八下&#x27;</span>;</span><br><span class="line">   <span class="built_in">console</span>.log(vm.msg,vm.$el.innerText) <span class="comment">// 输出 七上八下  一心一意</span></span><br><span class="line">   <span class="comment">// vm.msg的值确实改了,页面没有重新渲染,Dom没有变,证明渲染页面是异步执行的</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p> setTimeout 宏任务</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(vm.msg,vm.$el.innerText) <span class="comment">// 输出 七上八下  七上八下</span></span><br><span class="line">&#125;,<span class="number">0</span>)</span><br><span class="line"><span class="comment">//在渲染函数之后执行</span></span><br></pre></td></tr></table></figure>
<h5 id="vm-nextTick"><a href="#vm-nextTick" class="headerlink" title="vm.$nextTick()"></a>vm.$nextTick()</h5>Vue执行Dom操作完毕了之后会去执行$nextTick()里面的方法<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//比setTimeout执行更快</span></span><br><span class="line">vm.$nextTick(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">     <span class="built_in">console</span>.log(vm.msg,vm.$el.innerText) <span class="comment">// 输出 七上八下  七上八下</span></span><br><span class="line">   &#125;).then(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">     <span class="comment">//在这里打印一些值</span></span><br><span class="line">   &#125;)  </span><br><span class="line">    <span class="comment">//它是一个promise</span></span><br></pre></td></tr></table></figure></li>
</ol>
</li>
</ol>
<ul>
<li>Vue.nextTick<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"> Vue.nextTick(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(vm.msg,vm.$el.innerText) <span class="comment">// 输出 七上八下  七上八下</span></span><br><span class="line"> &#125;).then(<span class="function">()=&gt;</span>&#123;</span><br><span class="line"><span class="comment">//在这里打印一些值</span></span><br><span class="line"> &#125;) </span><br><span class="line"> <span class="comment">//它是一个promise</span></span><br></pre></td></tr></table></figure></li>
<li>vm.$nextTick() this vue实例</li>
<li>Vue.nextTick() this window<h4 id="关于Vue的缺点"><a href="#关于Vue的缺点" class="headerlink" title="关于Vue的缺点"></a>关于Vue的缺点</h4>由于Vue的渲染过程总是在等vue主线程执行完毕,再去渲染,当在主线程的某一个任务出问题了,或者卡住了,<br>异步任务就会一直等待主线程的任务,页面就会卡死,一直渲染不出来</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/06/15/%E5%89%96%E6%9E%90Vue%E7%9A%84%E5%93%8D%E5%BA%94%E5%BC%8F/" data-id="ckd8udtbz000i9gt468464ctd" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Vue/" rel="tag">Vue</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/06/17/Vue%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Vue的生命周期
        
      </div>
    </a>
  
  
    <a href="/2020/06/13/ES6%20Promise%E7%94%A8%E6%B3%95%E6%80%BB%E7%BB%93/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">ES6 Promise用法总结</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vue/" rel="tag">Vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vue%E5%85%A8%E5%AE%B6%E6%A1%B6/" rel="tag">Vue全家桶</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/js/" rel="tag">js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/node/" rel="tag">node</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Vue/" style="font-size: 20px;">Vue</a> <a href="/tags/Vue%E5%85%A8%E5%AE%B6%E6%A1%B6/" style="font-size: 10px;">Vue全家桶</a> <a href="/tags/js/" style="font-size: 15px;">js</a> <a href="/tags/node/" style="font-size: 10px;">node</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/07/">July 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">June 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/07/30/hello-world/">我的第一章博客</a>
          </li>
        
          <li>
            <a href="/2020/07/25/%E5%8E%9F%E7%94%9FHttp%E5%AE%9E%E7%8E%B0cookie%E8%AE%BE%E7%BD%AE/">原生Http实现cookie设置</a>
          </li>
        
          <li>
            <a href="/2020/07/23/%E5%B0%81%E8%A3%85Vue%E7%BB%84%E4%BB%B6%E5%B0%86%E4%BB%BB%E6%84%8F%E5%90%8E%E7%BC%80%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95%E7%BB%9F%E4%B8%80%E5%85%A8%E9%83%A8%E5%BC%95%E5%85%A5%E5%88%B0%E5%BD%93%E5%89%8D%E6%96%87%E4%BB%B6/">封装Vue组件将任意后缀文件目录统一全部引入到当前文件</a>
          </li>
        
          <li>
            <a href="/2020/07/15/js%E4%B8%AD%E7%9A%84%E9%98%B2%E6%8A%96%E4%B8%8E%E8%8A%82%E6%B5%81/">js中的防抖与节流</a>
          </li>
        
          <li>
            <a href="/2020/07/07/%E6%89%8B%E5%8A%A8%E5%AE%9E%E7%8E%B0call%EF%BC%8Capply,bind/">手动实现call，apply,bind</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>